package com.ruoyi.training.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.training.domain.ExamSchedule;
import com.ruoyi.training.dto.MyExamInfo;
import com.ruoyi.training.vo.ClassStudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.ClassStudent;
import com.ruoyi.training.service.IClassStudentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 班级学员明细Controller
 *
 * @author WorrilessGo
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/ClassStudent")
public class ClassStudentController extends BaseController
{
    @Autowired
    private IClassStudentService classStudentService;

    /**
     * 查询班级学员明细列表
     */
    @RequiresPermissions("ClassStudent:ClassStudent:list")
    @GetMapping("/list")
    public TableDataInfo list(ClassStudent classStudent)
    {
        startPage();
        List<ClassStudent> list = classStudentService.selectClassStudentList(classStudent);
        return getDataTable(list);
    }

    /**
     * 根据班级ID查询所有报名信息和学员信息
     * lqx
     */
//    @RequiresPermissions("@ss.hasPermi('training:classStudent:list')")
//    @GetMapping("/getClassStudents/{trainingClassId}")
//    public TableDataInfo getClassStudents(@PathVariable("trainingClassId") String trainingClassId) {
//        List<ClassStudentVO> result = classStudentService.getClassStudents(trainingClassId);
//        return getDataTable(result);
//    }
    /**
     * 根据班级ID查询所有报名信息和学员信息，支持多字段模糊搜索
     * lqx
     */
    @GetMapping("/getClassStudents/{trainingClassId}")
    public TableDataInfo getClassStudents(
            @PathVariable("trainingClassId") String trainingClassId,
            @RequestParam(required = false) String studentName,    // 姓名搜索
            @RequestParam(required = false) String phonenumber,    // 手机号码搜索
            @RequestParam(required = false) String idNumber) {     // 证件编号搜索

        List<ClassStudentVO> result = classStudentService.getClassStudents(
                trainingClassId, studentName, phonenumber, idNumber);
        return getDataTable(result);
    }

    /**
     * 导出班级学员明细列表
     */
    @RequiresPermissions("ClassStudent:ClassStudent:export")
    @Log(title = "班级学员明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClassStudent classStudent)
    {
        List<ClassStudent> list = classStudentService.selectClassStudentList(classStudent);
        ExcelUtil<ClassStudent> util = new ExcelUtil<ClassStudent>(ClassStudent.class);
        util.exportExcel(response, list, "班级学员明细数据");
    }

    /**
     * 获取班级学员明细详细信息
     */
    @RequiresPermissions("ClassStudent:ClassStudent:query")
    @GetMapping(value = "/{classStudentId}")
    public AjaxResult getInfo(@PathVariable("classStudentId") String classStudentId)
    {
        return success(classStudentService.selectClassStudentByClassStudentId(classStudentId));
    }

    /**
     * 新增班级学员明细
     */
    @RequiresPermissions("ClassStudent:ClassStudent:add")
    @Log(title = "班级学员明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClassStudent classStudent)
    {
        return toAjax(classStudentService.insertClassStudent(classStudent));
    }

    /**
     * 修改班级学员明细
     */
    @RequiresPermissions("ClassStudent:ClassStudent:edit")
    @Log(title = "班级学员明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClassStudent classStudent)
    {
        return toAjax(classStudentService.updateClassStudent(classStudent));
    }

    /**
     * 删除班级学员明细
     */
    @RequiresPermissions("ClassStudent:ClassStudent:remove")
    @Log(title = "班级学员明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classStudentIds}")
    public AjaxResult remove(@PathVariable String[] classStudentIds)
    {
        return toAjax(classStudentService.deleteClassStudentByClassStudentIds(classStudentIds));
    }

    /**
     * 批量进班（支持单个）
     * lqx
     */
    @PostMapping("/batchJoin")
    public AjaxResult batchJoin(@RequestBody List<ClassStudent> classStudentList)
    {
        return toAjax(classStudentService.batchJoin(classStudentList));
    }

    /**
     * 批量退班（支持单个）
     * lqx
     */
    @PostMapping("/batchQuit")
    public AjaxResult batchQuit(@RequestBody List<ClassStudent> classStudentList)
    {
        int rows = classStudentService.batchQuit(classStudentList);
        if (rows == 100){
            return AjaxResult.error("学员id为空");
        }
        return toAjax(rows);
    }

    /**
     * 通过学员id获取学员的考试情况
     * lqx
     */
    @GetMapping("/getExamInfoByStudentId/{studentId}")
    public TableDataInfo getExamInfoByStudentId(@PathVariable("studentId") String studentId) {
        List<MyExamInfo> result = classStudentService.selectExamInfoByStudentId(studentId);
        return getDataTable(result);
    }

    /**
     * 通过学员id获取学员的考试情况-后端获取id
     * lqx
     */
    @GetMapping("/getExamInfoByStudentId")
    public TableDataInfo getExamInfoByStudentId() {
        String studentId = SecurityUtils.getLoginUser().getSysUser().getStudentId();
        if (!"".equals(studentId) && studentId != null){
            List<MyExamInfo> result = classStudentService.selectExamInfoByStudentId(studentId);
            return getDataTable(result);
        }else{
            return null;
        }
    }

    /**
     * 通过学员id获取学员的考试安排
     * lqx
     */
    @GetMapping("/getExamScheduleByStudentId/{studentId}")
    public TableDataInfo getExamScheduleByStudentId(@PathVariable("studentId") String studentId,String trainingClassId) {
        List<ExamSchedule> result = classStudentService.selectExamScheduleByStudentId(studentId,trainingClassId);
        return getDataTable(result);
    }

    /**
     * 通过学员id获取学员的考试安排-后端获取id
     * lqx
     */
    @GetMapping("/getExamScheduleByStudentId")
    public TableDataInfo getExamScheduleByStudentId(String trainingClassId) {
        String studentId = SecurityUtils.getLoginUser().getSysUser().getStudentId();
        if (!"".equals(studentId) && studentId != null){
            List<ExamSchedule> result = classStudentService.selectExamScheduleByStudentId(studentId,trainingClassId);
            return getDataTable(result);
        }else{
            return null;
        }
    }
}
